package com.guoguo.utils.aliOss.operation;

import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.utils.Base64Utils;
import com.guoguo.utils.StringUtils;
import com.guoguo.utils.aliOss.AbstractAliOssImage;
import com.guoguo.utils.aliOss.AliOssImageConfig;

/**
 * Created by Administrator on 2017/10/25 0025.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */
public class AliOssImageWatermark extends AbstractAliOssImage {


    @Override
    public String operatingString(AliOssImageConfig aliOssImageConfig) {
        StringBuffer sb = new StringBuffer();

//        sb.append(imageStyle);

        sb.append("/").append(AliOssImageEnum.ALI_HANDLE_WATERMARK.getValue());

        // TODO: 2017/10/26 0026  图片不为空而文字为空的时候，使用图片水印
        if (!StringUtils.isEmptyString(aliOssImageConfig.getImage()) && StringUtils.isEmptyString(aliOssImageConfig.getText())) {
            sb.append(imageWatermarkUrl(aliOssImageConfig));
        }
        // TODO: 2017/10/26 0026  图片为空而文字不为空，使用文字水印
        if (StringUtils.isEmptyString(aliOssImageConfig.getImage()) && !StringUtils.isEmptyString(aliOssImageConfig.getText())) {
            sb.append(fontWatermarkUrl(aliOssImageConfig));
        }
        // TODO: 2017/10/26 0026  文字和图片都不为空的时候，使用混合水印
        if (!StringUtils.isEmptyString(aliOssImageConfig.getImage()) && !StringUtils.isEmptyString(aliOssImageConfig.getText())) {
            sb.append(imageWatermarkUrl(aliOssImageConfig));
            sb.append(fontWatermarkUrl(aliOssImageConfig));
            // TODO: 2017/10/26 0026 图文显示顺序
            if (aliOssImageConfig.getOrder() == AliOssImageEnum.ALI_HANDLE_ORDER_IMAGE.getId()) {
                sb.append(",").append(AliOssImageEnum.ALI_HANDLE_ORDER_IMAGE.getValue());
            } else {
                sb.append(",").append(AliOssImageEnum.ALI_HANDLE_ORDER_FONT.getValue());
            }
            // TODO: 2017/10/26 0026 对齐方式
            if (valiArgs(aliOssImageConfig.getAlign(), 0, 2))
                sb.append(",align_").append(aliOssImageConfig.getAlign());
            // TODO: 2017/10/26 0026 间距
            if (valiArgs(aliOssImageConfig.getInterval(), 1, 1000))
                sb.append(",interval_").append(aliOssImageConfig.getInterval());
        }
        // TODO: 2017/10/26 0026 透明度
        if (valiArgs(aliOssImageConfig.getTransparency(), 1, 100))
            sb.append(",t_").append(aliOssImageConfig.getTransparency());

        // TODO: 2017/10/26 0026 水印位置
        String pos = new String();
        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_NW.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_NW.getValue();
        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_NORTH.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_NORTH.getValue();
        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_NE.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_NE.getValue();

        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_WEST.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_WEST.getValue();
        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_CENTER.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_CENTER.getValue();
        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_EAST.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_EAST.getValue();

        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_SW.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_SW.getValue();
        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_SOUTH.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_SOUTH.getValue();
        if (aliOssImageConfig.getPositionType() == AliOssImageEnum.ALI_HANDLE_POSITION_SE.getId())
            pos = AliOssImageEnum.ALI_HANDLE_POSITION_SE.getValue();
        sb.append(",g_").append(pos);

        // TODO: 2017/10/26 0026 水印离图片边的边距
        if (valiArgs(aliOssImageConfig.getX(), 1, 4096))
            sb.append(",x_").append(aliOssImageConfig.getX());
        if (valiArgs(aliOssImageConfig.getY(), 1, 4096))
            sb.append(",y_").append(aliOssImageConfig.getY());

        // TODO: 2017/10/26 0026 垂直中线偏移量
        if (valiArgs(aliOssImageConfig.getVoffset(), -1000, 1000))
            sb.append(",voffset_").append(aliOssImageConfig.getVoffset());

        return sb.toString();
    }

    /**
     * 文字水印
     *
     * @param aliOssImageConfig
     * @return
     */
    private String fontWatermarkUrl(AliOssImageConfig aliOssImageConfig) {
        StringBuffer stringBuffer = new StringBuffer();
        // TODO: 2017/10/26 0026 文字内容
        stringBuffer.append(",text_").append(urlBase64Encoding(aliOssImageConfig.getText()));
        // TODO: 2017/10/26 0026 文字类型 
        if (!StringUtils.isEmptyString(aliOssImageConfig.getType()))
            stringBuffer.append(",type_").append(urlBase64Encoding(aliOssImageConfig.getType()));
        // TODO: 2017/10/26 0026 文字大小 
        if (valiArgs(aliOssImageConfig.getSize(), 1, 1000))
            stringBuffer.append(",size_").append(aliOssImageConfig.getSize());

        // TODO: 2017/10/26 0026 文字颜色
        if (!StringUtils.isEmptyString(aliOssImageConfig.getColor()))
            stringBuffer.append(",color_").append(aliOssImageConfig.getColor());
        // TODO: 2017/10/26 0026 文字透明度 
        if (valiArgs(aliOssImageConfig.getShadow(), 1, 100))
            stringBuffer.append(",shadow_").append(aliOssImageConfig.getShadow());

        return stringBuffer.toString();
    }

    /**
     * 图片水印
     *
     * @param aliOssImageConfig
     * @return
     */
    private String imageWatermarkUrl(AliOssImageConfig aliOssImageConfig) {
        StringBuffer url = new StringBuffer();
        StringBuffer stringBuffer = new StringBuffer();
        // TODO: 2017/10/26 0026 图片url
        stringBuffer.append(aliOssImageConfig.getImage());
        // TODO: 2017/10/26 0026 压缩类型
        if (aliOssImageConfig.getResizeType() != 0) {
            // TODO: 2017/10/26 0026 压缩大小
            if (valiArgs(aliOssImageConfig.getProportion(), 1, 100)) {
                StringBuffer sb = new StringBuffer();
                sb.append("?x-oss-process=image/resize,");
                sb.append("p_").append(aliOssImageConfig.getProportion());
                stringBuffer.append(sb);
            }
        }
        url.append(",image_").append(urlBase64Encoding(stringBuffer.toString()));
        return url.toString();
    }

    private String urlBase64Encoding(String content) {
        String str = Base64Utils.encode(content);
        str = str.replace("+", "-");
        str = str.replace("/", "_");
        str = str.replace("=", "");
        return str;
    }
}
